package com.meixiang.activity.account.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.account.SelectMemberStoreActivity;
import com.meixiang.entity.account.UserInfo;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.main.MXApplication;
import com.meixiang.main.MainActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.util.DesUtil;
import com.meixiang.view.ClearEditText;

import org.json.JSONObject;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 登录
 *
 * @author zys
 */
public class LoginActivity extends BaseActivity {
    /**
     * 手机号、邮箱
     */
    @Bind(R.id.cet_input_name)
    ClearEditText cet_input_name;
    /**
     * 密码
     */
    @Bind(R.id.cet_input_pwd)
    ClearEditText cet_input_pwd;
    /**
     * 登录按钮
     */
    @Bind(R.id.tv_login)
    TextView tv_login;
    /**
     * 忘记密码
     */
    @Bind(R.id.tv_forget_pwd)
    TextView tv_forget_pwd;
    /**
     * 注册账户
     */
    @Bind(R.id.tv_reg_account)
    TextView tv_reg_account;

    private Activity mActivity;
    //融云token
    private String token;

    private UserInfo mUserInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        mActivity = LoginActivity.this;
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initTitle() {
        setRightTitle("登录", "注册", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(context, RegisterActivity.class));
            }
        });

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {
    }

    @OnClick({R.id.tv_login, R.id.tv_forget_pwd, R.id.tv_reg_account})
    public void onClick(View view) {
        if(Tool.isFastDoubleClick()){
        return;
        }
        switch (view.getId()) {

            case R.id.tv_login:
                if (TextUtils.isEmpty(cet_input_name.getText().toString())) {
                    Tool.showTextToast(mActivity, "请输入手机号码");
                    return;
                }
                if (cet_input_pwd.getText().length() < 6 || cet_input_pwd.getText().length() > 16) {
                    Tool.showTextToast(mActivity, "请输入6~16位的密码");
                    return;
                }
                sendHttp();
                break;
            case R.id.tv_forget_pwd:
                startActivity(new Intent(context, ForgetPasswordActivity.class));
                break;
            case R.id.tv_reg_account:
                startActivity(new Intent(context, RegisterActivity.class));
                break;
        }
    }


    /**
     * 登录
     */
    private void sendHttp() {
//        password	String	必填			密码
//        username	String	必填			用户名
        HttpParams params = new HttpParams();
        params.put("username", cet_input_name.getText().toString().trim());
        params.put("password", DesUtil.md5(cet_input_pwd.getText().toString().trim()));
        HttpUtils.post(Config.LOGIN, params, new HttpCallBack(mActivity,"正在登录中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                HashMap<String, String> vals = new HashMap<String, String>();
                vals.put("jsessionid", result.optString("sessionId"));
                MXApplication.mApp.getSession().set(vals);
                AbToastUtil.showToast(LoginActivity.this, message);

                if (result.optInt("memberCardNum") > 1) {
                    startActivity(new Intent(context, SelectMemberStoreActivity.class));
                } else {
                    getUserinfo();
                    startActivity(MainActivity.class);
                }
                finish();
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(LoginActivity.this, message);
            }
        });


    }

    /**
     * 登录成功获取用户信息保存并且用户信息
     */
    private void getUserinfo() {
        HttpParams params = new HttpParams();

        HttpUtils.post(Config.USERINFO_URL, params, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                mUserInfo = AbJsonUtil.fromJson(result.toString(), UserInfo.class);
                MXApplication.mApp.setUser(mUserInfo);

                Log.e("TAG", "得到当前用户信息==" + MXApplication.mApp.getUser());

            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(activity, message);
            }
        });

    }

}
